package com.lottery.im.module.tim.presenter;

import com.lottery.im.module.tim.event.FriendshipEvent;
import com.lottery.im.module.tim.event.GroupEvent;
import com.lottery.im.module.tim.event.MessageEvent;
import com.lottery.im.module.tim.event.RefreshEvent;
import com.lottery.im.module.tim.viewfeatures.ConversationView;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.group.TIMGroupCacheInfo;
import com.tencent.imsdk.ext.message.TIMConversationExt;
import com.tencent.imsdk.ext.message.TIMManagerExt;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 会话界面逻辑
 */
public class ConversationPresenter implements Observer {

  private ConversationView view;

  public ConversationPresenter(ConversationView view) {
    //注册消息监听
    MessageEvent.getInstance().addObserver(this);
    //注册刷新监听
    RefreshEvent.getInstance().addObserver(this);
    //注册好友关系链监听
    FriendshipEvent.getInstance().addObserver(this);
    //注册群关系监听
    GroupEvent.getInstance().addObserver(this);
    this.view = view;
  }

  @Override
  public void update(Observable observable, Object data) {
    if (observable instanceof MessageEvent) {
      if (data instanceof TIMMessage) {
        TIMMessage msg = (TIMMessage) data;
        view.updateMessage(msg);
      } else {
        view.refresh();
      }
    } else if (observable instanceof FriendshipEvent) {
      FriendshipEvent.NotifyCmd cmd = (FriendshipEvent.NotifyCmd) data;
      switch (cmd.type) {
        case PROFILE_UPDATE:
        case ADD_REQ:
        case READ_MSG:
        case ADD:
          view.updateFriendshipMessage();
          break;
        case DEL:
          // 删除好友 同时删除对应的 会话
          view.removeAndConversation(TIMConversationType.C2C, ((List<String>) cmd.data).get(0));
          //view.removeConversation(((List<String>) cmd.data).get(0));
          break;
      }
    } else if (observable instanceof GroupEvent) {
      GroupEvent.NotifyCmd cmd = (GroupEvent.NotifyCmd) data;
      switch (cmd.type) {
        case UPDATE:
        case ADD:
          view.updateGroupInfo((TIMGroupCacheInfo) cmd.data);
          break;
        case DEL:
          view.removeAndConversation(TIMConversationType.Group, (String) cmd.data);
          //view.removeConversation((String) cmd.data);
          view.refresh();
          break;
      }
    } else if (observable instanceof RefreshEvent) {
      getConversation();
      view.refresh();
    }
  }

  public void getConversation() {
    List<TIMConversation> list = TIMManagerExt.getInstance().getConversationList();
    List<TIMConversation> result = new ArrayList<>();
    for (TIMConversation conversation : list) {
      if (conversation.getType() == TIMConversationType.System) continue;
      result.add(conversation);
      TIMConversationExt conversationExt = new TIMConversationExt(conversation);
      // getLocalMessage 本地缓存消息 getMessage 线上消息
      conversationExt.getLocalMessage(1, null, new TIMValueCallBack<List<TIMMessage>>() {
        @Override
        public void onError(int i, String s) {
        }

        @Override
        public void onSuccess(List<TIMMessage> timMessages) {
          if (timMessages.size() > 0) {
            view.updateMessage(timMessages.get(0));
          }
        }
      });
    }
    view.initConversationView(result);
  }

  /**
   * 删除会话
   *
   * @param type 会话类型
   * @param id 会话对象id
   */
  public boolean delConversation(TIMConversationType type, String id) {
    return TIMManagerExt.getInstance().deleteConversationAndLocalMsgs(type, id);
  }

  public void stop() {
    MessageEvent.getInstance().deleteObserver(this);
    RefreshEvent.getInstance().deleteObserver(this);
    FriendshipEvent.getInstance().deleteObserver(this);
    GroupEvent.getInstance().deleteObserver(this);
  }
}
